package com.naodong.shenluntiku.mvp.view.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.view.MenuItem;
import android.view.View;

import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.intergation.alipush.PushManager;
import com.naodong.shenluntiku.mvp.model.data.manager.TokenManager;
import com.naodong.shenluntiku.mvp.view.fragment.FindFragment;
import com.naodong.shenluntiku.mvp.view.fragment.MineFragment;
import com.naodong.shenluntiku.mvp.view.fragment.StudyFragment;
import com.yatatsu.autobundle.AutoBundleField;

import butterknife.BindView;
import me.shingohu.man.base.BaseMVPActivity;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.intergation.activitymanager.TheActivityManager;
import me.yokeyword.fragmentation.SupportFragment;

public class MainActivity extends BaseMVPActivity {


    @BindView(R.id.navigation)
    BottomNavigationView bottomNavigationView;

    int[] navigationIds = {R.id.navigation_one, R.id.navigation_two, R.id.navigation_third};

    SupportFragment[] mFragments = new SupportFragment[navigationIds.length];

    @AutoBundleField(required = false)
    int index = 0;


    @Override
    protected void onCreateInit(Bundle savedInstanceState) {
        init();
        initFragment(savedInstanceState);
        initView();
        setSelectTab(index);
        initPush();
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    private void init() {
        TokenManager.getInstance().saveToken();
        TheActivityManager.getInstance().toInstanceOf(this.getClass());
    }


    private void initPush() {
        PushManager.getInstance().bindAccount();
    }


    @Override
    protected boolean userAutoBindBundle() {
        return true;
    }

    protected void initFragment(Bundle savedInstanceState) {


        if (savedInstanceState == null) {
            mFragments[0] = StudyFragment.getInstance();
            mFragments[1] = FindFragment.getInstance();
            mFragments[2] = MineFragment.getInstance();
            loadMultipleRootFragment(R.id.content, index, mFragments);
        } else {
            mFragments[0] = findFragment(StudyFragment.class);
            mFragments[1] = findFragment(FindFragment.class);
            mFragments[2] = findFragment(MineFragment.class);
        }

    }

    private void initView() {
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            MenuItem fristMenu = bottomNavigationView.getMenu().findItem(R.id.navigation_one);
            MenuItem secontMenu = bottomNavigationView.getMenu().findItem(R.id.navigation_two);
            MenuItem thirdMenu = bottomNavigationView.getMenu().findItem(R.id.navigation_third);
            fristMenu.setIcon(R.drawable.navigation_rec_nor);
            secontMenu.setIcon(R.drawable.navigation_find_nor);
            thirdMenu.setIcon(R.drawable.navigation_me_nor);

            if (item == fristMenu) {
                item.setIcon(R.drawable.navigation_rec_select);
            }
            if (item == secontMenu) {
                item.setIcon(R.drawable.navigation_find_select);
            }
            if (item == thirdMenu) {
                item.setIcon(R.drawable.navigation_me_select);
            }
            showHideFragment(mFragments[item.getOrder()], mFragments[index]);
            index = item.getOrder();
            onTabSelect(index);
            return true;
        });

    }

    private void onTabSelect(int index) {

        if (index == 1) {
            setStatusBarViewVisibility(View.GONE);
            setToolBarViewVisibility(View.GONE);
        } else {
            setStatusBarViewVisibility(View.VISIBLE);
            setToolBarViewVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onBackPressedSupport() {
        moveTaskToBack(true);
    }

    private void setSelectTab(int i) {
        bottomNavigationView.setSelectedItemId(navigationIds[i]);
    }


    @Override
    public boolean supportActionbar() {
        return false;
    }

    @Override
    protected int layoutId() {
        return R.layout.a_main;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
